package superman.app.gr100.biz.gr_expapp;

import android.app.ListFragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedList;

import java.util.List;

import widget.PullToRefreshLayout;

/**
 * Created by Scor on 2015/12/3.
 */
public class FragmentOrderBower extends Fragment{
    private ListView myListView;
    private OrderBowerAdapter orderBowerAdapter;
    private LinkedList<Order> orderLinkedList = new LinkedList<>();
    private PullToRefreshLayout pullToRefreshLayout;
    private View view;
    private final int ADD_LIST = 1;
    private final int HTTP_RESQUEST = 2;
    private final int NULL_HTTP_RESQUEST = 3;
    private int page = 0;
    private int pageCount = 4;
    private String restURL;
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.d("scor","you get it");
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup viewGroup,Bundle savedInstanceState){
        view = inflater.inflate(R.layout.fragment_order_bower,viewGroup,false);
        pullToRefreshLayout = (PullToRefreshLayout) view.findViewById(R.id.refresh_view);
        pullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page = page + 1;
                restURL = "http://192.168.1.233:8080/findData/" + String.valueOf(page) + "/" + String.valueOf(pageCount);
                sendUrlRequest();
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        });
        myListView = (ListView) view.findViewById(R.id.content_view);
        return  view;
    }
    @Override
    public  void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        initListView();
    }
    final private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Log.d("scor","you get it");
            onListItemClick((ListView) parent, v, position, id);
        }
    };

    private void initListView() {
        orderLinkedList.add(new Order("果然100", 2000, "虹桥坊店", "扬州人民商场二楼3号柜台", "1449048299000", "wxpay", 20.50,"o123"));
        orderLinkedList.add(new Order("果然100", 2000, "虹桥坊店", "扬州人民商场二楼3号柜台", "1449048299000", "wxpay", 20.50,"p556"));
        orderBowerAdapter = new OrderBowerAdapter(getContext(), R.layout.bower_item,orderLinkedList);
        myListView.setAdapter(orderBowerAdapter);
    }

    private Handler handler = new Handler() {
        public void handleMessage(Message message){
            switch (message.what){
                case ADD_LIST:
                    orderLinkedList.add(new Order("果然100", 2000, "虹桥坊店", "扬州人民商场二楼3号柜台", "1449048299000", "wxpay", 20.50,"w561"));
                    orderBowerAdapter.notifyDataSetChanged();
                    break;
                case HTTP_RESQUEST:
                    JSONArray jsonArray = JSONArray.parseArray(String.valueOf(message.obj));
                    JSONObject ob;
                    for(int i=0;i<jsonArray.size();i++){
                        ob = (JSONObject) jsonArray.get(i);
                        orderLinkedList.add(new Order("果然100", 2000, "虹桥坊店", "扬州人民商场二楼3号柜台", "1449048299000", "wxpay", 20.50,"n541"));
                    }
                    orderBowerAdapter.notifyDataSetChanged();
                    break;
                case NULL_HTTP_RESQUEST:
                    Log.d("scor","no more");
                    break;
                default:
                    break;
            }
        }
    };
    private void sendUrlRequest(){
        new Thread(new Runnable(){
            @Override
            public void run() {
                HttpURLConnection connection = null;
                try{
                    Log.d("scor", String.valueOf(page));
                    URL url =new URL(restURL);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    InputStream inputStream = connection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                    StringBuilder respone = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null){
                        respone.append(line);
                    }
                    Message message = new Message();
//                    Log.d("scor", respone.toString());
                    JSONArray jsonArray = JSONArray.parseArray(String.valueOf(respone.toString()));
                    if(jsonArray.size() == 0){
                        message.what = NULL_HTTP_RESQUEST;
                    }else {
                        message.obj = respone.toString();
                        message.what = HTTP_RESQUEST;
//                        message.what = NULL_HTTP_RESQUEST;

                    }
                    handler.sendMessage(message);
                }catch(Exception e){

                }
            }
        }).start();
    }
    final private Runnable mRequestFocus = new Runnable() {
        public void run() {
            myListView.focusableViewAvailable(myListView);
        }
    };
}